package com.sudichina.sudichina.view.activity.dentification;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sudichina.sudichina.Contains;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.Car;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CarSelectActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.lv_car_select)
    ListView lvCarSelect;
    @BindView(R.id.activity_car_select)
    LinearLayout activityCarSelect;
    private List<Car> carListData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_select);
        ButterKnife.bind(this);

        try {
            carListData = getIntent().getParcelableArrayListExtra("carList");
        } catch (Exception e) {
        }


        initViewAndData();

    }

    private void initViewAndData() {
        tvTitle.setText("车辆管理");
        tvRight.setVisibility(View.VISIBLE);



        lvCarSelect.setAdapter(new MyAdapter());
        lvCarSelect.setOnItemClickListener(this);


    }

    @OnClick({R.id.iv_back, R.id.tv_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_right:
                startActivity(new Intent(this,CarIdentificationActivity.class));


                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent(this, CarDetailActivity.class);
        intent.putExtra(Contains.CAR_TO_DETAIL, carListData.get(position));
        startActivity(intent);
    }


    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return carListData.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyViewHolder myViewHolder = null;
            if (convertView == null) {
                convertView = View.inflate(parent.getContext(), R.layout.item_car_select, null);

                myViewHolder = new MyViewHolder(convertView);
                convertView.setTag(myViewHolder);
            } else {
                myViewHolder = (MyViewHolder) convertView.getTag();
            }
            Car car = carListData.get(position);
            if (car != null) {
                myViewHolder.tvItemChepai.setText(car.getCarFirstLetter() + car.getCardNumber());
                int auditStatus = car.getAuditStatus();
                switch (auditStatus) {
                    case 0:
                        myViewHolder.tvItemIcsState.setText("审核中 >");
                        break;
                    case 2:
                        myViewHolder.tvItemIcsState.setText("已通过 >");
                        break;
                    case 3:
                        myViewHolder.tvItemIcsState.setText("未通过 >");
                        break;
                }

            }


            return convertView;
        }

    }

    class MyViewHolder {
        @BindView(R.id.tv_item_chepai)
        TextView tvItemChepai;
        @BindView(R.id.tv_item_ics_state)
        TextView tvItemIcsState;

        MyViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


}
